package ua.ck.example.web;

import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import ua.ck.example.domain.Customer;
import ua.ck.example.domain.Employee;
import ua.ck.example.domain.Order;
import ua.ck.example.domain.OrderDetail;
import ua.ck.example.domain.Product;
import ua.ck.example.service.OrderService;
import ua.ck.example.service.ProductService;
import ua.ck.example.util.Constant;
import ua.ck.example.util.TimeUtil;
import ua.ck.example.web.dto.OrderDto;

@Controller
@RequestMapping(OrderController.URL)
public class OrderController {
	static final String URL = "/order";
	@Autowired
	private OrderService orderService;
	@Autowired
	private ProductService productService;

	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String listOrders(Model model) {

		model.addAttribute("listOrders", orderService.listOrders());
		return "order/index";

	}

	@RequestMapping(value = "/mine", method = RequestMethod.GET)
	public String mineOrders(Model model, HttpServletRequest req) {
		String type = (String) req.getSession().getAttribute(
				Constant.SESSION_KEY_USER_TYPE);
		// Order orderParam = new Order();
		if (type.equals(Constant.SESSION_CUSTOMER_TYPE)) {
			Customer customer = (Customer) req.getSession().getAttribute(
					Constant.SESSION_KEY_USER);
			Set<Order> set = (Set) customer.getOrders();
			for (Order order : set) {
				Set<OrderDetail> dSet = order.getOrderDetails();
				for (OrderDetail od : dSet) {
					od.getDiscount();
				}
			}
			model.addAttribute("listOrders", customer.getOrders());
			// orderParam.setCustomerId(customer.getCustomerId());

		} else if (type.equals(Constant.SESSION_EMPLOYEE_TYPE)) {
			Employee employee = (Employee) req.getSession().getAttribute(
					Constant.SESSION_KEY_USER);
			model.addAttribute("listOrders", employee.getOrders());
			// orderParam.setEmployeeId(employee.getEmpId());
		}
		// model.addAttribute("listOrders", orderService.getOrders(orderParam));
		model.addAttribute("type", type);
		return "order/mine";

	}

	@RequestMapping(value = "/del", method = RequestMethod.GET)
	public String delProducts(Model model,
			@RequestParam(value = "orderId", required = false) int orderId) {
		Order orderParam = new Order();
		orderParam.setOrderId(orderId);
		orderService.delOrder(orderParam);
		return "redirect:" + URL + "/mine";

	}

	@RequestMapping(value = "/addorder", method = RequestMethod.GET)
	public String addOrder(
			Model model,
			@RequestParam(value = "productId", required = false) Integer productId,
			@RequestParam(value = "quantity", required = false) Integer quantity,
			HttpServletRequest req) {
		Product productParam = new Product();
		productParam.setProductId(productId);
		Product productGet = productService.getProducts(productParam).get(0);
		OrderDto orderDto = new OrderDto();
		orderDto.setProductId(productId);
		orderDto.setDiscount(productGet.getSalesPrice());
		orderDto.setTitle(productGet.getProductName());
		orderDto.setQuantity(quantity);
		orderDto.setMaxQuantity(productGet.getQuantity());
		model.addAttribute("orderDto", orderDto);

		return "order/addorder";
	}

	@RequestMapping(value = "/addorder", method = RequestMethod.POST)
	public String addOrder(@ModelAttribute("orderDto") OrderDto orderDto,
			BindingResult result, HttpServletRequest req) {
		Order order = new Order();
		order.setOrderDate(TimeUtil.getCurrent());
		Customer customer = (Customer) req.getSession().getAttribute(
				Constant.SESSION_KEY_USER);
		order.setCustomer(customer);
		customer.getOrders().add(order);
		orderService.addOrder(order);

		OrderDetail orderDetail = new OrderDetail();
		orderDetail.setDiscount(orderDto.getDiscount());
		orderDetail.setQuantity(orderDto.getQuantity());
		Product productParam = new Product();
		productParam.setProductId(orderDto.getProductId());
		Product productGet = productService.getProducts(productParam).get(0);
		orderDetail.setProduct(productGet);
		productGet.getOrderDetail().add(orderDetail);
		orderDetail.setOrder(order);
		order.getOrderDetails().add(orderDetail);

		productGet.setQuantity(productGet.getQuantity()-orderDetail.getQuantity());
		orderService.addOrderDetail(orderDetail);

		return "redirect:" + URL + "/mine";

	}

	@RequestMapping(value = "/unconfirmOrders", method = RequestMethod.GET)
	public String unconfirmOrders(Model model) {
		model.addAttribute("listOrders", orderService.getUnconfirmOrders());
		return "order/unconfirmOrders";
	}

	@RequestMapping(value = "/confirmOrder", method = RequestMethod.GET)
	public String confirmOrder(
			@RequestParam(value = "orderId", required = false) Integer orderId,
			HttpServletRequest req) {
		Order orderParam = new Order();
		orderParam.setOrderId(orderId);
		Order orderGet = orderService.getOrders(orderParam).get(0);
		Employee employee = (Employee) req.getSession().getAttribute(
				Constant.SESSION_KEY_USER);
		orderGet.setEmployee(employee);
		employee.getOrders().add(orderGet);
		orderService.updateOrder(orderGet);
		return "redirect:" + URL + "/mine";
	}
}
